﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.W.realize;

namespace MyMES.Writer.API.Application.CommendHandlers.W.realize
{
    public class DelMaterialproductclassificationCommandHanders : IRequestHandler<DelMaterialproductclassificationCommands, APIResult<Materialproductclassification>>
    {
        private readonly IBaseRepository<Materialproductclassification> _repository;
        private readonly IMapper mapper;

        public DelMaterialproductclassificationCommandHanders(IBaseRepository<Materialproductclassification> repository, IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 删除物料产品信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Materialproductclassification>> Handle(DelMaterialproductclassificationCommands request, CancellationToken cancellationToken)
        {
            APIResult<Materialproductclassification> result = new APIResult<Materialproductclassification>();
            result.Code = APIEnum.删除成功;
            result.Msg = "删除成功";
            var map = mapper.Map<Materialproductclassification>(request);
            var list = $"Update Materialproductclassification set IsDelete=true where ParentId='{request.ParentId}'";
            await _repository.Update(map, list);
            return result;
        }
    }
}
